package com.school.service.impl;

import com.school.entity.ComplaintInfo;
import com.school.repository.ComplaintInfoRepository;
import com.school.service.ComplaintInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ComplaintInfoServiceImp implements ComplaintInfoService {
    @Autowired
    ComplaintInfoRepository complaintInfoRepository;

    /**
     * 增加
     * @param complaintInfo
     * @return
     */
    @Override
    public ComplaintInfo addComplaintInfo(ComplaintInfo complaintInfo) {
        complaintInfo.setCreateDate(new Date());
        complaintInfo.setUpdateDate(new Date());
        return complaintInfoRepository.save(complaintInfo);
    }

    /**
     * 修改
     * @param complaintInfo
     * @return
     */
    @Override
    public ComplaintInfo updateComplaintInfo(ComplaintInfo complaintInfo) {
        return complaintInfoRepository.saveAndFlush(complaintInfo);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public ComplaintInfo getDetailsById(String id) {
        return complaintInfoRepository.findById(Integer.valueOf(id)).get();
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteComplaintInfo(String id) {
        ComplaintInfo complaintInfo = null;
        try {
            complaintInfo = complaintInfoRepository.findById(Integer.valueOf(id)).get();
            complaintInfoRepository.delete(complaintInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据条件分页查询
     * @param complaintInfo,page,size
     */
    @Override
    public Page<ComplaintInfo> specificPageQueryComplaintInfoList(ComplaintInfo complaintInfo,int page, int size) {
        Specification querySpecification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(complaintInfo != null)
                {
                    String studentId = complaintInfo.getStudentId();
                    String auditStatus = complaintInfo.getAuditStatus();
                    String complainantsContent = complaintInfo.getComplainantsContent();
                    if (!StringUtils.isEmpty(studentId))
                    {
                        predicates.add(criteriaBuilder.equal(root.get("studentId").as(String.class),studentId));
                    }
                    if (!StringUtils.isEmpty(auditStatus))
                    {
                        predicates.add(criteriaBuilder.equal(root.get("auditStatus").as(String.class),auditStatus));
                    }
                    if (!StringUtils.isEmpty(complainantsContent))
                    {
                        predicates.add(criteriaBuilder.like(root.get("complainantsContent").as(String.class), "%" + complainantsContent + "%"));
                    }
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return complaintInfoRepository.findAll(querySpecification,PageRequest.of(page - 1, size));
    }

}
